class Solution
{
public:
    int maximumLength(vector<int> &nums)
    {
        map<int, int> count;
        for (auto num : nums)
        {
            count[num]++;
        }
        int maxResult = 0;
        unordered_set<int> seen;
        for (auto &[num, cnt] : count)
        {
            if (seen.count(num))
            {
                continue;
            }
            seen.insert(num);

            if (num == 1)
            {
                int result = cnt & 1 ? cnt : cnt - 1;
                maxResult = max(maxResult, result);
                continue;
            }

            if (cnt < 2)
            {
                maxResult = max(maxResult, 1);
                continue;
            }

            int result = 1;
            for (int k = 2; k < 32; k *= 2)
            {
                double nextD = pow(num, k);
                if (nextD > 1e9)
                {
                    break;
                }
                int next = nextD;
                seen.insert(next);

                int nextCount = count[next];

                if (nextCount == 0)
                {
                    break;
                }
                result += 2;
                maxResult = max(maxResult, result);
                if (nextCount == 1)
                {
                    break;
                }
            }
        }
        return maxResult;
    }
};